/**
 * Software License, Version 1.0
 * 
 * Copyright 2003 The Trustees of Indiana University.  All rights reserved.
 * 
 *
 *Redistribution and use in source and binary forms, with or without 
 *modification, are permitted provided that the following conditions are met:
 *
 *1) All redistributions of source code must retain the above copyright notice,
 * the list of authors in the original source code, this list of conditions and
 * the disclaimer listed in this license;
 *2) All redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the disclaimer listed in this license in
 * the documentation and/or other materials provided with the distribution;
 *3) Any documentation included with all redistributions must include the 
 * following acknowledgement:
 *
 *"This product includes software developed by the Community Grids Lab. For 
 * further information contact the Community Grids Lab at 
 * http://communitygrids.iu.edu/."
 *
 * Alternatively, this acknowledgement may appear in the software itself, and 
 * wherever such third-party acknowledgments normally appear.
 * 
 *4) The name Indiana University or Community Grids Lab or NaradaBrokering, 
 * shall not be used to endorse or promote products derived from this software 
 * without prior written permission from Indiana University.  For written 
 * permission, please contact the Advanced Research and Technology Institute 
 * ("ARTI") at 351 West 10th Street, Indianapolis, Indiana 46202.
 *5) Products derived from this software may not be called NaradaBrokering, 
 * nor may Indiana University or Community Grids Lab or NaradaBrokering appear
 * in their name, without prior written permission of ARTI.
 * 
 *
 * Indiana University provides no reassurances that the source code provided 
 * does not infringe the patent or any other intellectual property rights of 
 * any other entity.  Indiana University disclaims any liability to any 
 * recipient for claims brought by any other entity based on infringement of 
 * intellectual property rights or otherwise.  
 *
 *LICENSEE UNDERSTANDS THAT SOFTWARE IS PROVIDED "AS IS" FOR WHICH NO 
 *WARRANTIES AS TO CAPABILITIES OR ACCURACY ARE MADE. INDIANA UNIVERSITY GIVES
 *NO WARRANTIES AND MAKES NO REPRESENTATION THAT SOFTWARE IS FREE OF 
 *INFRINGEMENT OF THIRD PARTY PATENT, COPYRIGHT, OR OTHER PROPRIETARY RIGHTS. 
 *INDIANA UNIVERSITY MAKES NO WARRANTIES THAT SOFTWARE IS FREE FROM "BUGS", 
 *"VIRUSES", "TROJAN HORSES", "TRAP DOORS", "WORMS", OR OTHER HARMFUL CODE.  
 *LICENSEE ASSUMES THE ENTIRE RISK AS TO THE PERFORMANCE OF SOFTWARE AND/OR 
 *ASSOCIATED MATERIALS, AND TO THE PERFORMANCE AND VALIDITY OF INFORMATION 
 *GENERATED USING SOFTWARE.
 */
package cgl.narada.test.linkdemo;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 *
 * @author Xi Rao
 */
public class SslPanel {
    private JPanel sslPanel;
    private JLabel trustStoreLabel;
    private JTextField trustStoreInputField;
    private JLabel keyStoreLabel;
    private JTextField keyStoreInputField;
    private JLabel trustStorePasswordLabel;
    private JPasswordField trustStorePasswordInputField;
    private JLabel keyStorePasswordLabel;
    private JPasswordField keyStorePasswordInputField;
    private JLabel usernameLabel;
    private JTextField usernameInputField;
    private JLabel passwordLabel;
    private JPasswordField passwordInputField;
    private JLabel domainLabel;
    private JTextField domainInputField;
    private JLabel hostLabel;
    private JTextField hostInputField;
    private JLabel httpProxyHostLabel;
    private JTextField httpProxyHostInputField;
    private JLabel httpProxyPortLabel;
    private JTextField httpProxyPortInputField;

    //radio button
    private JLabel securityLabel;
    private ButtonGroup group;
    private JRadioButton trueButton;
    private JRadioButton falseButton;
    private JLabel listenerPortLabel;
    private JTextField listenerPortInputField;
    private JLabel remoteHostLabel;
    private JTextField remoteHostInputField;
    private JLabel remotePortLabel;
    private JTextField remotePortInputField;
    private JButton loadServiceButton;
    private JButton createLinkButton;

    private Properties sslFacProperty;
    private Properties sslLinkProperty;

    private String defaultSecurity = "true";
    private Communicator communicator = LinkCommunicatorGui.getCommunicator();

    public SslPanel() {
        sslFacProperty = new Properties();
        sslLinkProperty = new Properties();

        sslPanel = new JPanel(new GridBagLayout());

        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(2, 2, 2, 2);

        GridBagLayoutAssistant gridBagAssistant = new GridBagLayoutAssistant(gridBagConstraints,
                                                                             sslPanel);
        trustStoreLabel = new JLabel("Trust store");
        gridBagAssistant.add(trustStoreLabel, GridBagConstraints.NONE, 0, 0, 0.0, 1);
        trustStoreInputField = new JTextField("c:\\keys\\truststore",15);
        gridBagAssistant.add(trustStoreInputField, GridBagConstraints.NONE, 1, 0, 0.0, 3);
        keyStoreLabel = new JLabel("Key store");
        gridBagAssistant.add(keyStoreLabel, GridBagConstraints.NONE, 4, 0, 0.0, 1);
        keyStoreInputField = new JTextField("c:\\keys\\keystore",15);
        gridBagAssistant.add(keyStoreInputField, GridBagConstraints.NONE, 5, 0, 0.0, 3);

        trustStorePasswordLabel = new JLabel("Trust store password");
        gridBagAssistant.add(trustStorePasswordLabel, GridBagConstraints.NONE, 0, 1, 0.0, 1);
        trustStorePasswordInputField = new JPasswordField("geoffrey",15);
        gridBagAssistant.add(trustStorePasswordInputField, GridBagConstraints.NONE, 1, 1, 0.0, 3);
        keyStorePasswordLabel = new JLabel("Key store password");
        gridBagAssistant.add(keyStorePasswordLabel, GridBagConstraints.NONE, 4, 1, 0.0, 1);
        keyStorePasswordInputField = new JPasswordField("geoffrey",15);
        gridBagAssistant.add(keyStorePasswordInputField, GridBagConstraints.NONE, 5, 1, 0.0, 3);

        usernameLabel = new JLabel("User Name");
        gridBagAssistant.add(usernameLabel, GridBagConstraints.NONE, 0, 2, 0.0, 1);
        usernameInputField = new JTextField("sc02",15);
        gridBagAssistant.add(usernameInputField, GridBagConstraints.NONE, 1, 2, 0.0, 3);
        passwordLabel = new JLabel("Password");
        gridBagAssistant.add(passwordLabel, GridBagConstraints.NONE, 4, 2, 0.0, 1);
        passwordInputField = new JPasswordField("sc02",15);
        gridBagAssistant.add(passwordInputField, GridBagConstraints.NONE, 5, 2, 0.0, 3);

        domainLabel = new JLabel("Domain");
        gridBagAssistant.add(domainLabel, GridBagConstraints.NONE, 0, 3, 0.0, 1);
        domainInputField = new JTextField("grids",15);
        gridBagAssistant.add(domainInputField, GridBagConstraints.NONE, 1, 3, 0.0, 3);
        hostLabel = new JLabel("NTLM Host");
        gridBagAssistant.add(hostLabel, GridBagConstraints.NONE, 4, 3, 0.0, 1);
        hostInputField = new JTextField("nile",15);
        gridBagAssistant.add(hostInputField, GridBagConstraints.NONE, 5, 3, 0.0, 3);

        httpProxyHostLabel = new JLabel("http.proxy host");
        gridBagAssistant.add(httpProxyHostLabel, GridBagConstraints.NONE, 0, 4, 0.0, 1);
        httpProxyHostInputField = new JTextField("nile",15);
        gridBagAssistant.add(httpProxyHostInputField, GridBagConstraints.NONE, 1, 4, 0.0, 3);
        httpProxyPortLabel = new JLabel("http.proxy port");
        gridBagAssistant.add(httpProxyPortLabel, GridBagConstraints.NONE, 4, 4, 0.0, 1);
        httpProxyPortInputField = new JTextField("8080",15);
        gridBagAssistant.add(httpProxyPortInputField, GridBagConstraints.NONE, 5, 4, 0.0, 3);

        group = new ButtonGroup();
        trueButton = new JRadioButton("true", true);
        trueButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                defaultSecurity = "true";
            }
        });
        falseButton = new JRadioButton("false", false);
        falseButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                defaultSecurity = "false";
            }
        });
        group.add(trueButton);
        group.add(falseButton);
        securityLabel = new JLabel("enable security");
        gridBagAssistant.add(securityLabel, GridBagConstraints.NONE, 0, 5, 0.0, 1);
        gridBagAssistant.add(trueButton, GridBagConstraints.NONE, 1, 5, 0.0, 1);
        gridBagAssistant.add(falseButton, GridBagConstraints.NONE, 2, 5, 0.0, 1);

        listenerPortLabel = new JLabel("Listener port");
        gridBagAssistant.add(listenerPortLabel, GridBagConstraints.NONE, 4, 5, 0.0, 1);
        listenerPortInputField = new JTextField("443",15);
        gridBagAssistant.add(listenerPortInputField, GridBagConstraints.NONE, 5, 5, 0.0, 3);

        remoteHostLabel = new JLabel("remote host");
        gridBagAssistant.add(remoteHostLabel, GridBagConstraints.NONE, 0, 6, 0.0, 1);
        remoteHostInputField = new JTextField("deccan.ucs.indiana.edu",15);
        gridBagAssistant.add(remoteHostInputField, GridBagConstraints.NONE, 1, 6, 0.0, 3);
        remotePortLabel = new JLabel("remote port");
        gridBagAssistant.add(remotePortLabel, GridBagConstraints.NONE, 4, 6, 0.0, 1);
        remotePortInputField = new JTextField("443",15);
        gridBagAssistant.add(remotePortInputField, GridBagConstraints.NONE, 5, 6, 0.0, 3);

        loadServiceButton = new JButton("Load Service");
        gridBagAssistant.add(loadServiceButton, GridBagConstraints.SOUTH, 2, 7, 0.0, 2);
        createLinkButton = new JButton("Create link ");
        createLinkButton.setEnabled(false);
        gridBagAssistant.add(createLinkButton, GridBagConstraints.SOUTH, 4, 7, 0.0, 2);

        loadServiceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                loadServiceAction();
            }
        });
        createLinkButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createLinkAction();
            }
        });
        setWidgetStatus(true);
        setWidgetStatus2(true);
    }

    public JPanel getPanel() {
        return sslPanel;
    }

    public void loadServiceAction() {
        System.out.println("in the load service");
        sslFacProperty.setProperty("truststore",trustStoreInputField.getText());
        sslFacProperty.setProperty("keystore",keyStoreInputField.getText());
        sslFacProperty.setProperty("truststorePassword",new String(trustStorePasswordInputField.getPassword()));
        sslFacProperty.setProperty("keystorePassword",new String(keyStorePasswordInputField.getPassword()));
        sslFacProperty.setProperty("username",usernameInputField.getText());
        sslFacProperty.setProperty("password", new String(passwordInputField.getPassword()));
        sslFacProperty.setProperty("domain",domainInputField.getText());
        sslFacProperty.setProperty("host",hostInputField.getText());
        sslFacProperty.setProperty("https.proxyHost",httpProxyHostInputField.getText());
        sslFacProperty.setProperty("https.proxyPort",httpProxyPortInputField.getText());
        sslFacProperty.setProperty("secure",defaultSecurity);
        sslFacProperty.setProperty("listenerport",listenerPortInputField.getText());


        try {
            communicator.loadCommunicationsOfType(sslFacProperty, "ssl");
        } catch(cgl.narada.transport.TransportException transEx) {
            String errorInfo = transEx.toString();
            GuiButtonPanel.getGuiButtonPanel().setResponse(errorInfo);
            return;
        }

        setWidgetStatus(false);
        setWidgetStatus2(false);
        this.createLinkButton.setEnabled(true);
    }

    public void createLinkAction() {

        sslLinkProperty.setProperty("host",remoteHostInputField.getText());
        sslLinkProperty.setProperty("port",remotePortInputField.getText());

        try{
            String linkid = communicator.createLinkTo(sslLinkProperty,"ssl");
            GuiTopPanel.getGuiTopPanel().setLinkId(linkid);
        }catch(cgl.narada.transport.TransportException transEx) {
            String errorInfo = transEx.toString();
            GuiButtonPanel.getGuiButtonPanel().setResponse(errorInfo);
            return;
        }

        setWidgetStatus(false);
        setWidgetStatus2(true);
    }

    private void setWidgetStatus(boolean isWorking){
        trustStoreInputField.setEditable(isWorking);
        keyStoreInputField.setEditable(isWorking);
        trustStorePasswordInputField.setEditable(isWorking);
        keyStorePasswordInputField.setEditable(isWorking);
        usernameInputField.setEditable(isWorking);
        passwordInputField.setEditable(isWorking);
        domainInputField.setEditable(isWorking);
        hostInputField.setEditable(isWorking);
        httpProxyHostInputField.setEditable(isWorking);
        httpProxyPortInputField.setEditable(isWorking);
        trueButton.setEnabled(isWorking);
        falseButton.setEnabled(isWorking);
        listenerPortInputField.setEditable(isWorking);
        createLinkButton.setEnabled(!isWorking);
    }
    private void setWidgetStatus2(boolean isWorking){
        remoteHostInputField.setEditable(!isWorking);
        remotePortInputField.setEditable(!isWorking);
        loadServiceButton.setEnabled(isWorking);

    }
}